#include "param.h"
#include "types.h"
#include "defs.h"
#include "x86.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "elf.h"


//EG: debug tool
#define NONE_ALGO 0
#define FIFO_ALGO 1
#define NFU_ALGO  2

#define PRINT_DEBUG 0
int init_swap = 0;
static int isPaging = 0;


//End EG


extern char data[];  // defined by kernel.ld
pde_t *kpgdir;  // for use in scheduler()
struct segdesc gdt[NSEGS];

// Set up CPU's kernel segment descriptors.
// Run once on entry on each CPU.
void
seginit(void)
{
  struct cpu *c;

  // Map "logical" addresses to virtual addresses using identity map.
  // Cannot share a CODE descriptor for both kernel and user
  // because it would have to have DPL_USR, but the CPU forbids
  // an interrupt from CPL=0 to DPL=3.
  c = &cpus[cpunum()];
  c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0);
  c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
  c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER);
  c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER);

  // Map cpu, and curproc
  c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 8, 0);

  lgdt(c->gdt, sizeof(c->gdt));
  loadgs(SEG_KCPU << 3);
  
  // Initialize cpu-local storage.
  cpu = c;
  proc = 0;
}

// Return the address of the PTE in page table pgdir
// that corresponds to virtual address va.  If alloc!=0,
// create any required page table pages.
static pte_t *
walkpgdir(pde_t *pgdir, const void *va, int alloc)
{
  pde_t *pde;
  pte_t *pgtab;

  pde = &pgdir[PDX(va)];
  if(*pde & PTE_P){
    pgtab = (pte_t*)p2v(PTE_ADDR(*pde));
  } else {
    if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
      return 0;
    // Make sure all those PTE_P bits are zero.
    memset(pgtab, 0, PGSIZE);
    // The permissions here are overly generous, but they can
    // be further restricted by the permissions in the page table 
    // entries, if necessary.
    *pde = v2p(pgtab) | PTE_P | PTE_W | PTE_U;
  }

  return &pgtab[PTX(va)];
}

// Create PTEs for virtual addresses starting at va that refer to
// physical addresses starting at pa. va and size might not
// be page-aligned.
static int
mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
{

  //SHOW_DEBUG("va: %x\n", va);

  char *a, *last;
  pte_t *pte;
  
  a = (char*)PGROUNDDOWN((uint)va);
  last = (char*)PGROUNDDOWN(((uint)va) + size - 1);

  for(;;){
    if((pte = walkpgdir(pgdir, a, 1)) == 0)
      return -1;
    if(*pte & PTE_P)
      panic("remap");
    *pte = pa | perm | PTE_P;
    if(a == last)
      break;
    a += PGSIZE;
    pa += PGSIZE;
  }
  return 0;
}

// There is one page table per process, plus one that's used when
// a CPU is not running any process (kpgdir). The kernel uses the
// current process's page table during system calls and interrupts;
// page protection bits prevent user code from using the kernel's
// mappings.
// 
// setupkvm() and exec() set up every page table like this:
//
//   0..KERNBASE: user memory (text+data+stack+heap), mapped to
//                phys memory allocated by the kernel
//   KERNBASE..KERNBASE+EXTMEM: mapped to 0..EXTMEM (for I/O space)
//   KERNBASE+EXTMEM..data: mapped to EXTMEM..V2P(data)
//                for the kernel's instructions and r/o data
//   data..KERNBASE+PHYSTOP: mapped to V2P(data)..PHYSTOP, 
//                                  rw data + free physical memory
//   0xfe000000..0: mapped direct (devices such as ioapic)
//
// The kernel allocates physical memory for its heap and for user memory
// between V2P(end) and the end of physical memory (PHYSTOP)
// (directly addressable from end..P2V(PHYSTOP)).

// This table defines the kernel's mappings, which are present in
// every process's page table.
static struct kmap {
  void *virt;
  uint phys_start;
  uint phys_end;
  int perm;
} kmap[] = {
  { (void*) KERNBASE, 0,             EXTMEM,    PTE_W},  // I/O space
  { (void*) KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kernel text+rodata
  { (void*) data,     V2P(data),     PHYSTOP,   PTE_W},  // kernel data, memory
  { (void*) DEVSPACE, DEVSPACE,      0,         PTE_W},  // more devices
};

// Set up kernel part of a page table.
pde_t*
setupkvm()
{
  pde_t *pgdir;
  struct kmap *k;

  if((pgdir = (pde_t*)kalloc()) == 0)
    return 0;
  memset(pgdir, 0, PGSIZE);
  if (p2v(PHYSTOP) > (void*)DEVSPACE)
    panic("PHYSTOP too high");
  for(k = kmap; k < &kmap[NELEM(kmap)]; k++)
    if(mappages(pgdir, k->virt, k->phys_end - k->phys_start, 
                (uint)k->phys_start, k->perm) < 0)
      return 0;
  return pgdir;
}

// Allocate one page table for the machine for the kernel address
// space for scheduler processes.
void
kvmalloc(void)
{
  kpgdir = setupkvm();
  switchkvm();
}

// Switch h/w page table register to the kernel-only page table,
// for when no process is running.
void
switchkvm(void)
{
  lcr3(v2p(kpgdir));   // switch to the kernel page table
}

// Switch TSS and h/w page table to correspond to process p.
void
switchuvm(struct proc *p)
{
  pushcli();
  cpu->gdt[SEG_TSS] = SEG16(STS_T32A, &cpu->ts, sizeof(cpu->ts)-1, 0);
  cpu->gdt[SEG_TSS].s = 0;
  cpu->ts.ss0 = SEG_KDATA << 3;
  cpu->ts.esp0 = (uint)proc->kstack + KSTACKSIZE;
  ltr(SEG_TSS << 3);
  if(p->pgdir == 0)
    panic("switchuvm: no pgdir");
  lcr3(v2p(p->pgdir));  // switch to new address space
  popcli();
}

// Load the initcode into address 0 of pgdir.
// sz must be less than a page.
void
inituvm(pde_t *pgdir, char *init, uint sz)
{
  char *mem;
  
  if(sz >= PGSIZE)
    panic("inituvm: more than a page");
  mem = kalloc();
  memset(mem, 0, PGSIZE);
  mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U);
  memmove(mem, init, sz);
}

// Load a program segment into pgdir.  addr must be page-aligned
// and the pages from addr to addr+sz must already be mapped.
int
loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
{
  uint i, pa, n;
  pte_t *pte;

  if((uint) addr % PGSIZE != 0)
    panic("loaduvm: addr must be page aligned");
  for(i = 0; i < sz; i += PGSIZE){

    //SHOW_DEBUG("loaduvm: %x\n", addr+i);

    if((pte = walkpgdir(pgdir, addr+i, 0)) == 0)
      panic("loaduvm: address should exist");
    pa = PTE_ADDR(*pte);
    if(sz - i < PGSIZE)
      n = sz - i;
    else
      n = PGSIZE;
    if(readi(ip, p2v(pa), offset+i, n) != n)
      return -1;
  }
  return 0;
}

// Allocate page tables and physical memory to grow process from oldsz to
// newsz, which need not be page aligned.  Returns new size or 0 on error.
int
allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
  char *mem;
  uint a;
  
  // EG: set paging algorithm
  int selection_algorithm = NFU_ALGO;
  
#ifdef NONE
  selection_algorithm = NONE_ALGO;
#endif
  
#ifdef FIFO
  selection_algorithm = FIFO_ALGO;
#endif
  // End EG

  //SHOW_DEBUG("selection_algorithm: %d\n", selection_algorithm);
  
  //cprintf("algo: %d\n", selection_algorithm);
  
  if(newsz >= KERNBASE)
    return 0;
  if(newsz < oldsz)
    return oldsz;
  
  a = PGROUNDUP(oldsz);
  
  //TODO: add policy

  //SHOW_DEBUG("A_UVM: pid: %d, pim_b: %d\n", proc->pid, proc->page_md.pages_in_mem);
  
  for(; a < newsz; a += PGSIZE) {
    
    if ((selection_algorithm != NONE_ALGO) &&
	(proc->page_md.pages_in_mem >= MAX_PSYC_PAGES)) {
      swap_to_file(pgdir);
    }
    
    mem = kalloc();
    if(mem == 0){
      cprintf("allocuvm out of memory\n");
      deallocuvm(pgdir, newsz, oldsz);
      return 0;
    }
    memset(mem, 0, PGSIZE);
    mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U);

    
    //EG:
    if (selection_algorithm != NONE_ALGO) {
      add_va_page_table(PGROUNDDOWN(a));
    }
    
    //End EG

    
    //SHOW_DEBUG("allocuvn: pid: %d, pim: %d\n", proc->pid, proc->page_md.pages_in_mem);
  }
  return newsz;
}

// Deallocate user pages to bring the process size from oldsz to
// newsz.  oldsz and newsz need not be page-aligned, nor does newsz
// need to be less than oldsz.  oldsz can be larger than the actual
// process size.  Returns the new process size.
int
deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
{
  pte_t *pte;
  uint a, pa;

  if(newsz >= oldsz)
    return oldsz;

  a = PGROUNDUP(newsz);
  for(; a  < oldsz; a += PGSIZE){
    pte = walkpgdir(pgdir, (char*)a, 0);
    if(!pte)
      a += (NPTENTRIES - 1) * PGSIZE;
    else if((*pte & PTE_P) != 0){
      pa = PTE_ADDR(*pte);
      if(pa == 0)
        panic("kfree");
      char *v = p2v(pa);
      kfree(v);
      *pte = 0;
    }
  }
  return newsz;
}

// Free a page table and all the physical memory pages
// in the user part.
void
freevm(pde_t *pgdir)
{
  uint i;

  if(pgdir == 0)
    panic("freevm: no pgdir");
  deallocuvm(pgdir, KERNBASE, 0);
  for(i = 0; i < NPDENTRIES; i++){
    if(pgdir[i] & PTE_P){
      char * v = p2v(PTE_ADDR(pgdir[i]));
      kfree(v);
    }
  }
  kfree((char*)pgdir);
}

// Clear PTE_U on a page. Used to create an inaccessible
// page beneath the user stack.
void
clearpteu(pde_t *pgdir, char *uva)
{
  pte_t *pte;

  pte = walkpgdir(pgdir, uva, 0);
  if(pte == 0)
    panic("clearpteu");
  *pte &= ~PTE_U;
}

// Given a parent process's page table, create a copy
// of it for a child.
pde_t*
copyuvm(pde_t *pgdir, uint sz)
{

  pde_t *d;
  pte_t *pte;
  uint pa, i;
  char *mem;

  if((d = setupkvm()) == 0)
    return 0;
  for(i = 0; i < sz; i += PGSIZE){
    if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
      panic("copyuvm: pte should exist");
    if(!(*pte & PTE_P) && !(*pte & PTE_PG)) //not P and not PG
      panic("copyuvm: page not present");

    // EG: handle PG case
    if ((*pte & PTE_P) && !(*pte & PTE_PG)) { //P but not PG
      pa = PTE_ADDR(*pte);
      if((mem = kalloc()) == 0)
	goto bad;
      memmove(mem, (char*)p2v(pa), PGSIZE);
      if(mappages(d, (void*)i, PGSIZE, v2p(mem), PTE_W|PTE_U) < 0)
	goto bad;
    }
    else { //not P but PG (in file)
      if (map_swap_pages(d, (void*)i, PGSIZE, PTE_W|PTE_U) < 0)
	goto bad;
    }
  }
  
  return d;

bad:
  freevm(d);
  return 0;
}

//PAGEBREAK!
// Map user virtual address to kernel address.
char*
uva2ka(pde_t *pgdir, char *uva)
{
  pte_t *pte;

  pte = walkpgdir(pgdir, uva, 0);
  if((*pte & PTE_P) == 0)
    return 0;
  if((*pte & PTE_U) == 0)
    return 0;
  return (char*)p2v(PTE_ADDR(*pte));
}

// Copy len bytes from p to user address va in page table pgdir.
// Most useful when pgdir is not the current page table.
// uva2ka ensures this only works for PTE_U pages.
int
copyout(pde_t *pgdir, uint va, void *p, uint len)
{
  char *buf, *pa0;
  uint n, va0;

  buf = (char*)p;
  while(len > 0){
    va0 = (uint)PGROUNDDOWN(va);
    pa0 = uva2ka(pgdir, (char*)va0);
    if(pa0 == 0)
      return -1;
    n = PGSIZE - (va - va0);
    if(n > len)
      n = len;
    memmove(pa0 + (va - va0), buf, n);
    len -= n;
    buf += n;
    va = va0 + PGSIZE;
  }
  return 0;
}



int swap_to_file(pde_t *pgdir) {
  
  uint va_to_swap;
  uint *page_file_tbl;
  struct file *f;
  int i;
  pte_t *pte;
  char* page_va;
  if ((!isPaging) || (!isValidProc())) {
    return -1;
  }
  
  if (proc->page_md.pages_in_file >= MAX_FILE_PAGES) {
    
    //SHOW_DEBUG("pid: %d, pim: %d, pif: %d\n", proc->pid, proc->page_md.pages_in_mem, proc->page_md.pages_in_file);
    panic("Cannot swap to file, Swap file is full!");
  }
  
  if ((va_to_swap = get_page_to_swap(pgdir)) == -1) {
    return -1; //no page to swap
  }
  
  page_file_tbl = proc->page_md.pageFile_table;
  for (i=0; i<MAX_FILE_PAGES; i++) {
    if (page_file_tbl[i] == -1) {
      page_file_tbl[i] = va_to_swap;
      break;
    }
  }
  
  if (i == MAX_FILE_PAGES) {
    panic("File is full.\n");
  }
  
  //check pt[i]==va
  
  f = proc->page_md.page_file;
  
  set_file_offset(f, ((uint) i * PGSIZE));
  
  filewrite(f, (char*)va_to_swap, PGSIZE);
  
  //proc->page_md.pages_in_file++;
  
  if ((pte = walkpgdir(pgdir, (char*)va_to_swap, 0)) == 0) {
    panic("Page to swap is not exists.");
  }
  
  *pte &= ~PTE_P;
  *pte |= PTE_PG;
  page_va = p2v(PTE_ADDR(*pte));
  
  kfree(page_va);
  
  proc->page_md.pages_in_file++;
  proc->page_md.total_po++;
  proc->page_md.pages_in_mem--;
  
  /*
    SHOW_DEBUG("process %d move page %d to file\n",proc->pid,i);
    SHOW_DEBUG("TO file: page<%d>\n", i);
    for(i=0;i<15;i++)
    SHOW_DEBUG("page[%d] has nfu val %d\n",i,proc->page_md.nfuFile_table[i]);
  */
  
  return 0;
}


int swap_from_file(uint va) {
  //SHOW_DEBUG("SFF\n");
  
  char *mem;
  //uint va_to_swap;
  uint *page_file_tbl;
  struct file *f;
  int i;
  pte_t *pte;
  //char* page_va
  
 
  va = PGROUNDDOWN((uint)va);
  
  page_file_tbl = proc->page_md.pageFile_table;
  
  for (i=0; i<MAX_FILE_PAGES; i++) {
    if (page_file_tbl[i] == va) {
      page_file_tbl[i] = -1;
      break;
    }
  }
  
  //not found in file
  if (i == MAX_FILE_PAGES) {
    panic("Page is not in swap file"); // to check!!!
  }
  
  //check for consistency

  f = proc->page_md.page_file;
  set_file_offset(f, ((uint)i * PGSIZE));
  
  //allocate memory for page
  if ((mem = kalloc()) == 0) {
    panic("can't swap from file'\n");
    return 0;
  }

  memset(mem, 0, PGSIZE);
  
  if ((pte = walkpgdir(proc->pgdir, (char*)va, 0)) == 0) {
    panic("swap_from_file : page table not found");
  }

  // read from file to mem
  fileread(f, mem, PGSIZE);
  *pte &= 0xFFF;
  *pte |= v2p(mem);
  *pte &= (~PTE_PG);
  *pte |= PTE_P;

  proc->page_md.pages_in_file--;
  
  add_va_page_table(va);
  //SHOW_DEBUG("process %d move page %d from file\n",proc->pid,i);
  return 0;
}

void start_paging() {
  isPaging = 1;
}


int add_va_page_table(uint va) {
  
  /*
  if (proc->pid > 2) {
    SHOW_DEBUG("addVPT: pid: %d, pim_b: %d, pif: %d,  va: %d\n", proc->pid, proc->page_md.pages_in_mem, proc->page_md.pages_in_file , va);
  }
  */

  int i;

  for(i=0; i<MAX_PSYC_PAGES; i++) {
    
    //SHOW_DEBUG("VPT: mem[%d]= %d\n", i, proc->page_md.memFile_table[i]);

    if (proc->page_md.memFile_table[i] == -1) {
      proc->page_md.memFile_table[i] = va;
      proc->page_md.pages_in_mem++;
      
      SHOW_DEBUG("before_nfu[%d]= %x\n", i, proc->page_md.nfuFile_table[i]);

      proc->page_md.nfuFile_table[i] = (1 << 31); //TO CHECK!!!
      //proc->page_md.nfuFile_table[i] = 0;
      //proc->page_md.nfuFile_table[i] = 0xFFFFFFFF;
      
      SHOW_DEBUG("after_nfu[%d]= %x\n", i, proc->page_md.nfuFile_table[i]);

      return i;
    }
  }
  panic("Physical memory is full!!!");
}

int pageFault_handler(uint va) {
  //SHOW_DEBUG("PFH\n");
  proc->page_md.pagefault++;
  swap_to_file(proc->pgdir);
  swap_from_file(va);
  return 0;
}


uint get_fifo_to_swap() {
  uint va;
  uint index;
  
  index = proc->page_md.page_to_swap;
  va = proc->page_md.memFile_table[index];

  //SHOW_DEBUG("index: %d, va: %d\n", index, va);
  
  if (va == -1) {
    panic("Wrong va - check it!");
  }

  proc->page_md.memFile_table[index] = -1;
  index = (index + 1) % MAX_PSYC_PAGES;         
  proc->page_md.page_to_swap = index;
  return va;
}



uint get_nfu_to_swap() {
  //SHOW_DEBUG("nfu_swap: pid: %d\n", proc->pid);
  
  int i;
  uint min_val;
  int min_idx;
  uint va;
  
  i = min_idx = 0;

  min_val = proc->page_md.nfuFile_table[i];
  for(; i<MAX_PSYC_PAGES; i++) {
    if ((proc->page_md.memFile_table[i] != -1) &&
	(proc->page_md.nfuFile_table[i] < min_val)) {
      min_idx = i;
      min_val = proc->page_md.nfuFile_table[i];
    }
  }

  //SHOW_DEBUG("nfu_swap: pid: %d\n", proc->pid);


  va = proc->page_md.memFile_table[min_idx];
  proc->page_md.memFile_table[min_idx] = -1;

  return va;
}



uint get_page_to_swap(pde_t *pgdir) {
  
// EG: set paging algorithm
  int selection_algorithm = NFU_ALGO;

#ifdef NONE
  selection_algorithm = NONE_ALGO;
#endif

#ifdef FIFO
  selection_algorithm = FIFO_ALGO;
#endif
  // End EG
  
  if (selection_algorithm == NONE_ALGO) {
    return -1;
  }

  if (selection_algorithm == FIFO_ALGO) {
    return get_fifo_to_swap();
  }

  return get_nfu_to_swap();
}


int
map_swap_pages(pde_t *pgdir, void *va, uint size, int perm)
{
  char *a, *last;
  pte_t *pte;
  
  a = (char*)PGROUNDDOWN((uint)va);
  last = (char*)PGROUNDDOWN(((uint)va) + size - 1);
  for(;;){
    if((pte = walkpgdir(pgdir, a, 1)) == 0)
      return -1;
    if((*pte & PTE_P) || (*pte & PTE_PG))
      panic("remap");
    
    *pte = perm | PTE_PG;
    if(a == last)
      break;
    a += PGSIZE;
  }
  return 0;
}


int upDate_nfu_forProc(struct proc *p) {

  // SHOW_DEBUG("upnfp.\n");

  int i;
  pte_t *pte;
  uint *nfu_tbl;
  uint *mem_file_tbl;
  
  
  mem_file_tbl = p->page_md.memFile_table;
  nfu_tbl = p->page_md.nfuFile_table;
  
  for(i=0; i<MAX_PSYC_PAGES; i++) {

    //SHOW_DEBUG("A1_proc_before: %d, nfu[%d]: %x\n", p->pid, i, p->page_md.nfuFile_table[i]);
    nfu_tbl[i] >>= 1;
    //SHOW_DEBUG("A2_proc_before: %d, nfu[%d]: %x\n", p->pid, i, p->page_md.nfuFile_table[i]);

    if ((mem_file_tbl[i] != -1) &&
	((pte = walkpgdir(p->pgdir, (char*)mem_file_tbl[i], 0)) != 0) &&
	(*pte & PTE_A)) {
      
      SHOW_DEBUG("B_proc_before: %d, nfu[%d]: %x\n", p->pid, i, p->page_md.nfuFile_table[i]);

      *pte &= ~(PTE_A);
      nfu_tbl[i] |= (1 << 31);

      SHOW_DEBUG("C_proc_after: %d, nfu[%d]: %x\n", p->pid, i, p->page_md.nfuFile_table[i]);

    }
  }
  return 0;
}
